package org.lanqiao.controller.information;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiImplicitParams;
import org.lanqiao.config.entity.ResponseData;
import org.lanqiao.information.Answer;
import org.lanqiao.information.Comment;
import org.lanqiao.information.IAnswerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/information/answer")
@Api(tags = "回复Answer模块API")
public class AnswerController {
    @Reference
    IAnswerService answerService;

    @PostMapping("/add")
    @ApiOperation(value = "求助帖新增回复", notes = "回复某一个求助帖")
    @ApiImplicitParam(name = "answer", value = "回答answer实体", required = true, paramType = "query", dataTypeClass = Answer.class)
    public ResponseData<Answer> add(@RequestBody Answer answer) {
        try {
            answer.setAnswerTime(new Date());
            answerService.add(answer);
            return ResponseData.success();
        } catch (Exception e) {
            return ResponseData.serverInternalError();
        }
    }

    @GetMapping("/logicallyDelete")
    @ApiOperation(value = "根据id逻辑删除回复", notes = "逻辑删除某一个回复")
    @ApiImplicitParam(name = "ansId", value = "回复实体的主键ansId", required = true, paramType = "query", dataTypeClass = Answer.class)
    public ResponseData<Answer> logicallyDelete(@RequestParam Integer ansId) {
        try {
            answerService.logicallyDelete(ansId);
            return ResponseData.success();
        } catch (Exception e) {
            return ResponseData.serverInternalError();
        }
    }


    @GetMapping("/physicallyDelete")
    @ApiOperation(value = "根据id物理删除回复", notes = "物理删除某一个回复")
    @ApiImplicitParam(name = "ansId", value = "回复实体的主键ansId", required = true, paramType = "query", dataTypeClass = Answer.class)
    public ResponseData<Answer> physicallyDelete(@RequestParam Integer ansId) {
        try {
            answerService.physicallyDelete(ansId);
            return ResponseData.success();
        } catch (Exception e) {
            return ResponseData.serverInternalError();
        }
    }

/*
    @GetMapping("/findAnswerById")
    @ApiOperation(value = "根据ansId查找回复", notes = "根据主键ansId查找具体的某一回复")
    @ApiImplicitParam(name = "ansId", value = "回复的主键ansId", required = true, paramType = "query", dataTypeClass = Integer.class)
    public ResponseData<Answer> findAnswerById(@RequestParam Integer ansId) {
        try {
            Answer answer = answerService.findAnswerById(ansId);
            return ResponseData.success().putDataVule("answer", answer);
        } catch (Exception e) {
            return ResponseData.serverInternalError();
        }
    }*/

    @GetMapping("/findAll")
    @ApiOperation(value = "查找所有回复", notes = "查找数据库中的所有回复")
    public ResponseData<Answer> findAll() {
        try {
            // 实际应该从session中获取用户的id 然后使用用户的id查询

//            Integer userId = Integer.parseInt(String.valueOf(session.getAttribute("userId")));
            Map map = new HashMap();
//            map.put("userId", userId)
//            List<Answer> answerList = answerService.findAll();
            List<Answer> answerList = answerService.findList(map);
            System.out.println(answerList);
            return ResponseData.success().putDataVule("answerList", answerList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }

    @GetMapping("/findAll1")
    @ApiOperation(value = "查找所有回复", notes = "查找数据库中的所有回复")
    public ResponseData<Answer> findAll(@RequestParam Integer userId) {
        try {
            List<Answer> answerList = answerService.findAll(userId);
            return ResponseData.success().putDataVule("answerList", answerList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }

    @GetMapping("/pageAll")
    @ApiOperation(value = "分页查找所有回复", notes = "分页查找数据库中的所有回复")
    @ApiImplicitParams({
                @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, paramType = "query", dataTypeClass = Integer.class),
                @ApiImplicitParam(name = "pageSize", value = "单页的数据个数", required = true, paramType = "query", dataTypeClass = Integer.class)
            })
    public ResponseData<Answer> pageAll(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        try {
            IPage<Answer> page = answerService.pageAll(pageNum, pageSize);
            return ResponseData.success().putDataVule("answerList", page.getRecords()).putDataVule("pages", page.getPages()).putDataVule("total", page.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }

    @PostMapping("/pageList")
    @ApiOperation(value = "分页条件查找回复", notes = "分页条件查找数据库中的回复，至少包含一个条件," +
            "条件为ansId,content,status,informUserId,informContent,askId,userId,orderby")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "单页的数据个数", required = true, paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "queryMap", value = "查询条件", required = true, paramType = "body", dataType = "Map")
    })
    public ResponseData<Answer> pageList(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestBody Map<String, String> queryMap) {
        try {
            IPage<Answer> page = answerService.pageList(pageNum, pageSize, queryMap);
            return ResponseData.success().putDataVule("answerList", page.getRecords()).putDataVule("pages", page.getPages()).putDataVule("total", page.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }

/*
    @GetMapping("/pageListByUserId")
    @ApiOperation(value = "分页根据用户查找回复", notes = "分页条件根据用户id查找数据库中的回复")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "单页的数据个数", required = true, paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "userId", value = "用户od", required = true, paramType = "query", dataTypeClass = Integer.class)
    })
    public ResponseData<Answer> pageList(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam Integer userId) {
        try {
            IPage<Answer> page = answerService.pageListByUserId(pageNum, pageSize, userId);
            return ResponseData.success().putDataVule("answerList", page.getRecords()).putDataVule("pages", page.getPages()).putDataVule("total", page.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }
*/

    @PostMapping("/pageListByUserId")
    @ApiOperation(value = "分页根据用户查找回复", notes = "分页条件根据用户id查找数据库中的回复")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "单页的数据个数", required = true, paramType = "query", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "queryMap", value = "查询条件", required = true, paramType = "body", dataType = "Map")
    })
    public ResponseData<Answer> pageListByUserId(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestBody Map<String, String> queryMap) {
        try {
            IPage<Answer> page = answerService.pageListByUserId(pageNum, pageSize, queryMap);
            return ResponseData.success().putDataVule("answerList", page.getRecords()).putDataVule("pages", page.getPages()).putDataVule("total", page.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }

    @PostMapping("/findList")
    @ApiOperation(value = "条件查找回复", notes = "条件查找数据库中的部分回复," +
            "askId、content、status、informUserId、informContent、userId、likedCount" +
            " 排序使用orderby 例如orderby:answer_time，orderby:liked_count...")
    @ApiImplicitParam(name = "queryMap", value = "查询条件", required = true, paramType = "body", dataType = "Map")
    public ResponseData<Answer> findList(@RequestBody Map<String, String> queryMap) {
        try {
            List<Answer> answerList = answerService.findList(queryMap);
            return ResponseData.success().putDataVule("answerList", answerList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }



    @PostMapping("/toggleLike")
    @ApiOperation(value = "点赞或不点赞", notes = "to great or not great， that is a question")
    @ApiImplicitParam(name = "queryMap", value = "查询条件answerId、userId 这两个必须有", required = true, paramType = "body", dataType = "Map")
    public ResponseData<Answer> toggleLike(@RequestBody Map<String, String> queryMap) {
        try {
            answerService.toggleLike(queryMap);
            return ResponseData.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }


    @PostMapping("/inform")
    @ApiOperation(value = "举报回复", notes = "举报求助帖，需要回复主键ansId，举报人informUserId，举报原因informContent，缺一不可")
    @ApiImplicitParam(name = "informMap", value = "举报条件", required = true, paramType = "body", dataType = "Map")
    public ResponseData<Answer> inform(@RequestBody Map<String, String> informMap) {
        try {
            answerService.inform(informMap);
            return ResponseData.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }



    @GetMapping("/informSuccess")
    @ApiOperation(value = "举报回复成功", notes = "举报成功某一个回复，需要评论主键ansId")
    @ApiImplicitParam(name = "ansId", value = "举报的评论ansId", required = true, paramType = "query", dataTypeClass = Integer.class)
    public ResponseData<Comment> informSuccess(@RequestParam Integer ansId) {
        try {
            answerService.informSuccess(ansId);
            return ResponseData.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }



    @GetMapping("/informFail")
    @ApiOperation(value = "举报回复失败", notes = "举报失败某一个回复，需要回复主键ansId")
    @ApiImplicitParam(name = "ansId", value = "举报条件", required = true, paramType = "query", dataTypeClass = Integer.class)
    public ResponseData<Comment> informFail(@RequestParam Integer ansId) {
        try {
            answerService.informFail(ansId);
            return ResponseData.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }


}
